package com.geovis.boot.service.demo.file.security;


import com.geovis.boot.service.demo.file.entity.SysUser;
import com.geovis.boot.service.demo.file.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserService userService;




    @Override
    public UserDetails loadUserByUsername(final String userName) throws UsernameNotFoundException {
        SysUser sysUser = userService.queryByUserName(userName);
        if (sysUser == null) {

            throw new UsernameNotFoundException(userName + "该用户信息不存在");
//            throw new BadCredentialsException("该用户信息不存在！");

        }else{
            // 具体具有什么的权限
            List<GrantedAuthority> authList = new ArrayList<>();
//            AuthorityUtils.commaSeparatedStringToAuthorityList("user:view");
/*
            List<String> list =  userService.queryRoleByUserName(userName);
            for (String list1 : list) {
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(list1);
                authList.add(grantedAuthority);
            }*/
//            authList.add(new SimpleGrantedAuthority(roleService.getRoleNameByUserName(userName)));
            return new User(sysUser.getUserName(),sysUser.getPassword(),authList);

        }
    }

}


